<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Order;
use App\Models\Order as OrderModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = Order::with(['user', 'couponCode']);
        return Grid::make($model, function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type')->using(OrderModel::$typeMap)->label([
                'default'                     => 'primary',
                OrderModel::TYPE_NORMAL       => 'primary',
                OrderModel::TYPE_CROWDFUNDING => 'danger',
            ]);
            $grid->column('no');
            $grid->column('user.name', '所属用户');
            // $grid->column('address');
            $grid->column('total_amount');
            // $grid->column('remark');
            $grid->column('paid_at');
            $grid->column('couponCode.name', '优惠券');
            // $grid->column('payment_method');
            // $grid->column('payment_no');
            $grid->column('refund_status')->using(OrderModel::$refundStatusMap)->label([
                'default'                            => 'primary',
                OrderModel::REFUND_STATUS_PENDING    => 'primary',
                OrderModel::REFUND_STATUS_APPLIED    => 'danger',
                OrderModel::REFUND_STATUS_PROCESSING => 'info',
                OrderModel::REFUND_STATUS_SUCCESS    => 'success',
                OrderModel::REFUND_STATUS_FAILED     => 'warning',
            ]);
            // $grid->column('refund_no');
            $grid->column('closed')->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->column('reviewed')->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->column('ship_status')->using(OrderModel::$shipStatusMap)->label([
                'default'                         => 'primary',
                OrderModel::SHIP_STATUS_PENDING   => 'primary',
                OrderModel::SHIP_STATUS_DELIVERED => 'danger',
                OrderModel::SHIP_STATUS_RECEIVED  => 'success',
            ]);
            // $grid->column('ship_data');
            // $grid->column('extra');
            $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('no');
            $show->field('user_id');
            $show->field('address');
            $show->field('total_amount');
            $show->field('remark');
            $show->field('paid_at');
            $show->field('coupon_code_id');
            $show->field('payment_method');
            $show->field('payment_no');
            $show->field('refund_status');
            $show->field('refund_no');
            $show->field('closed');
            $show->field('reviewed');
            $show->field('ship_status');
            $show->field('ship_data');
            $show->field('extra');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('type');
            $form->text('no');
            $form->text('user_id');
            $form->text('address');
            $form->text('total_amount');
            $form->text('remark');
            $form->text('paid_at');
            $form->text('coupon_code_id');
            $form->text('payment_method');
            $form->text('payment_no');
            $form->text('refund_status');
            $form->text('refund_no');
            $form->text('closed');
            $form->text('reviewed');
            $form->text('ship_status');
            $form->text('ship_data');
            $form->text('extra');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->header('查看订单')
            // body 方法可以接受 Laravel 的视图作为参数
            ->body(view('admin.orders.show', ['order' => OrderModel::find($id)]));
    }
}
